/* 
* @Author: janmi
* @Date:   2015-07-04 17:22:00
* @Last Modified by:   anchen
* @Last Modified time: 2015-09-14 00:00:48
*/


var gulp = require('gulp'),
    less = require('gulp-less'),
    jshint = require('gulp-jshint'), 
    uglify = require('gulp-uglify'),
    autoprefixer = require('gulp-autoprefixer'),
    plumber = require('gulp-plumber'), 
    browserSync = require("browser-sync"), 
    changed = require('gulp-changed'),
    imgmin = require('gulp-imagemin'),
    pngmin = require('imagemin-pngquant'),
    cache = require('gulp-cache'),
    css = require('gulp-minify-css'),
    cssver = require('gulp-make-css-url-version'),
    clean = require('gulp-clean'),
    rev = require('gulp-rev'),//给静态资源添加hash版本号
    base64 = require('gulp-base64'),//将css中的图片用base64编码把图片数据翻译成标准ASCII字符,可以减少请求
    img64 = require('gulp-img64'),//将html中的图片用base64编码把图片数据翻译成标准ASCII字符 
    revCollector = require('gulp-rev-collector');//根据re生成的json文件替换模版文件的引用路径


    var SRC = 'src/';
    var DEST = 'dist/';
    var project = ['project','project2','project3'];

    gulp.task('less', function(){
        for (var i = 0; i < project.length; i++) {
            var path = SRC+project[i];
            gulp.src(path+'/less/**/*.less', {base:path+'/less'})
                .pipe(plumber())
                .pipe(changed(path+'/css',{extension: '.css'}))
                .pipe(less())
                .pipe(autoprefixer('last 2 versions'))
                .pipe(gulp.dest(path+'/css'))
                .pipe(browserSync.stream());
        };
    });

    gulp.task('watch', function(){
        for (var i = 0; i < project.length; i++) {
            var path = SRC+project[i]+"/less/*.less";
            gulp.watch(path,['less']);
        };
    });

    gulp.task('server', ['less'], function(){
        browserSync.init({
            files: 'src/**/*.*',
            server: {
                baseDir: '.'
            }
        });
        for (var i = 0; i < project.length; i++) {
            var path = SRC+project[i]+"/less/*.less";
            gulp.watch(path,['less']);
            gulp.watch('*.html').on('change', browserSync.reload);
        };
    });



    /* 打包任务 */

    gulp.task('cleanCss', function(){
        for (var i = 0; i < project.length; i++) {
            var destPath = DEST+project[i];
            gulp.src(destPath+'/css/*')
                .pipe(clean());
        };
    });
    gulp.task('cleanJs', function(){
        for (var i = 0; i < project.length; i++) {
            var destPath = DEST+project[i];
            gulp.src(destPath+'/js/*')
                .pipe(clean());
        };
    });

    gulp.task('mincss', ['cleanCss'], function(){
        for (var i = 0; i < project.length; i++) {
            var path = SRC+project[i];
            var destPath = DEST+project[i];
            gulp.src(path+'/css/**/*.css')
                .pipe(cssver())
                .pipe(css({compatibility:'ie7'}))
                .pipe(rev())
                .pipe(gulp.dest(destPath+'/css'))
                .pipe(rev.manifest())
                .pipe(gulp.dest(destPath+'/css/json'));
        };
    });

    //需要考虑合并
    gulp.task('rev', function(){
        for (var i = 0; i < project.length; i++) {
            var destPath = DEST+project[i];
            gulp.src([destPath+'/**/*.json',destPath+'/html/**/*.html'])//可以是php文件也可以是jsp文件
                .pipe(revCollector({replaceReved: true}))
                .pipe(gulp.dest(destPath+'/html'));
        }
    });

    gulp.task('revSea', function(){
        for (var i = 0; i < project.length; i++) {
            var destPath = DEST+project[i];
            gulp.src([destPath+'/**/*.json',destPath+'/js/**/*.js'])//可以是php文件也可以是jsp文件
                .pipe(revCollector({replaceReved: true}))
                .pipe(gulp.dest(destPath+'/js'));
        }
    });
    
    gulp.task('minjs', ['cleanJs'], function(){
        for (var i = 0; i < project.length; i++) {
            var path = SRC+project[i];
            var destPath = DEST+project[i];
            gulp.src(path+'/js/**/*.js')
                .pipe(uglify({mangle:false}))//不压缩变量名
                .pipe(rev())
                .pipe(gulp.dest(destPath+'/js'))
                .pipe(rev.manifest())
                .pipe(gulp.dest(destPath+'/js/json'));
        };
    });

    gulp.task('minimg',function(){
        for (var i = 0; i < project.length; i++) {
            var path = SRC+project[i];
            var destPath = DEST+project[i];
            gulp.src(path+'/img/**/*.{png,jpg,gif}')
                .pipe(cache(imgmin({use:[pngmin()]})))
                .pipe(gulp.dest(destPath+'/img'));
            gulp.src(path+'/font/**/*.{eot,svg,ttf,woff}')
                .pipe(cache(imgmin({use:[pngmin()]})))
                .pipe(gulp.dest(destPath+'/font'));
        };
    });


    gulp.task('default',['minjs','mincss']);
    
    /* 其他任务 */

    //建议使用IDE或者开发者工具来检查语法或者js错误，jshint会精确提供错误信息的所在位置，但是它不可以调式代码，要善用开发者工具来调式代码
    gulp.task('jshint', function(){
        gulp.src(SRC+'project/js/**/*.js')
            .pipe(jshint())
            .pipe(jshint.reporter('default'))
    });

    gulp.task('baseCss', function(){
        gulp.src('src/project/css/test1.css')
            .pipe(base64({
            //baseDir: 'src/project/base64',
            extensions: ['svg', 'png', /\.png#datauri$/i],
            exclude:    [/\.server\.(com|net)\/dynamic\//, '--live.jpg'],
            // maxImageSize: 8*1024, // bytes,
            deleteAfterEncoding: false,
            debug: true}))
            .pipe(gulp.dest('src/project/css'));
    })

    gulp.task('baseImg', function(){
        gulp.src('src/project/html/index.html')
            .pipe(img64())
            .pipe(gulp.dest('src/project/html'));
    })